package com.blb.orderservice.controller;

import com.blb.orderservice.pojo.Order;
import com.blb.orderservice.pojo.Product;
import com.blb.orderservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RefreshScope
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 查询所有订单
     * @return
     */
    @GetMapping("/orders")
    public ResponseEntity<List<Order>> getAllOrders(){
        List<Order> orders = orderService.list();
        orders.forEach(order -> {
            //调用商品微服务的接口查询商品
            ResponseEntity<Product> product = restTemplate.getForEntity(
                    "http://product-service/product/" + order.getProductId(),
                    Product.class);
            order.setProduct(product.getBody());
        });
        return ResponseEntity.ok(orders);
    }

    @Value("${spring.datasource.url}")
    private String url;

    @GetMapping("/order/url")
    public String getURL(){
        return url;
    }
}
